
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;


/**
 * Servlet implementation class Convert
 */
@WebServlet("/Convert")
public class Convert extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Convert() {
        super();
 
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
       PrintWriter out = response.getWriter();
        try {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Convertisseur</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<FORM Method=\"POST\" Action=\"http:\\\\localhost:8080\\CAR1_Convertisseur_20110929\\Convert\">");
            out.println("Valeur : <INPUT type=\"double\" size=20 name=Valeur><br>"); 
            out.println("<INPUT type=radio name=\"sens\" value=\"EF\"> : Euros -> Francs" +
            		"<br>" +
            		"<INPUT type=radio name=\"sens\" value=\"FE\"> : Francs -> Euros<br>");
            out.println("<INPUT type=\"submit\" value=\"Envoyer\"><br>");
            out.println("</FORM>");
            out.println("</body>");
            out.println("</html>");
            
        } finally {            
            out.close();
        }
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		try {
			
        double d = Double.parseDouble(request.getParameter("Valeur"));
			double q=0;
			String e = request.getParameter("sens");
			if(e.equals("EF"))
				q=d*6.55957;
			if(e.equals("FE"))
				q=d/6.55957;
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Convertisseur</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println(d);
            if(e.equals("EF"))
            	out.println(" euros");
            if(e.equals("FE"))
            	out.println(" francs");
            out.println(" = "+ q);
            if(e.equals("EF"))
            	out.println(" francs");
            if(e.equals("FE"))
            	out.println(" euros");
            out.println("</body>");
            out.println("</html>");
            
        } finally {            
            out.close();
        }

	}
	}
